package cn.kxx.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

//        取出cookie
        Cookie[] cookies = request.getCookies();

        if (cookies!=null&&cookies.length>0){
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                if (name.equals("login_member_telephone")){
                    String value = cookie.getValue();
                    System.out.println("登入cookie=========》"+value);
                    if (!StringUtils.isEmpty(value))
                    {
                        return true;
                    }
                }

            }
        }

        response.sendRedirect("/pages/login.html");
        return false;
    }
}
